package com.mercury.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mercury.user.entity.CenterUserWant;
import com.mercury.user.service.CenterUserWantService;
import com.mercury.util.R;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tong
 * @since 2021-11-14
 */
@RestController
@RequestMapping("/user/center-user-want")
@CrossOrigin
public class CenterUserWantController {

    @Autowired
    private CenterUserWantService wantService;

    /**
     * @date 2021/11/15 10:07
     * @params userWant
     * @return
     * @description 用户标记他想要喝的咖啡
    */
    @ApiOperation(value = "标记想要喝的咖啡")
    @PostMapping("/signWantCoffee")
    public R signWantCoffee(@RequestBody CenterUserWant userWant){
        boolean save = wantService.save(userWant);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }

    }

    @ApiOperation(value = "删除想要喝的咖啡")
    @DeleteMapping("/unSignWantCoffee")
    public R unSignWantCoffee(@RequestBody CenterUserWant userWant){
        QueryWrapper<CenterUserWant> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userWant.getUserId());
        wrapper.eq("coffee_id", userWant.getCoffeeId());
        boolean remove = wantService.remove(wrapper);
        if(remove) {
            return R.ok().message("取消标记成功");
        }else{
            return R.error().message("取消标记失败");
        }

    }


    @ApiOperation("获得用户想喝的咖啡的数量")
    @GetMapping("/getTasedCount")
    private R getTastedNum(@PathVariable String userId){
        int count = wantService.getCount(userId);
        return R.ok().data("count",count);
    }
}

